package com.qhong.converter;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.charset.Charset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

/**
 * Created by qhong on 2018/6/1 14:39
 **/
//@Configuration
public class DecodeDataHttpMessageConveter extends AbstractHttpMessageConverter<Object> {

	public static final ObjectMapper writeMapper = new ObjectMapper();

	public static final Logger LOG = LoggerFactory.getLogger(MappingJackson2HttpMessageConverter.class);



	public DecodeDataHttpMessageConveter() {
		super(new MediaType("application", "json", Charset.forName("UTF-8")));
	}

	@Override
	protected boolean supports(Class<?> aClass) {
		return true;
	}

	@Override
	protected Object readInternal(Class<?> aClass, HttpInputMessage httpInputMessage)
			throws IOException, HttpMessageNotReadableException {
		return null;
	}

	@Override
	protected void writeInternal(Object o, HttpOutputMessage httpOutputMessage)
			throws IOException, HttpMessageNotWritableException {
//		DataResponse d = (DataResponse) o;
//		if (o != null && o != SysConstant.NULL_RESPONSE) {
//			LOG.info("====>>>>> 响应数据：response={}", d.toString());
//			if (globalConfig.isEncryptFlag()) {
//				try {
//					String json = ToJson(d.getData());
//					long repStart = System.currentTimeMillis();
//					String data = CoderUtil.encryptAES(json);
//					d.setData(data);
//					long repEnd = System.currentTimeMillis();
//					LOG.info("加密耗时==>" + (repEnd - repStart) + " ms");
//				} catch (Exception e) {
//					LOG.error("加密失败");
//					e.printStackTrace();
//					//throw new InfoRuntimeException("999","结果加密失败");
//				}
//			}
//		}
//		byte[] jsonBytes = ToJsonAsBytes(d);
//		OutputStream out = httpOutputMessage.getBody();
//		out.write(jsonBytes, 0, jsonBytes.length);
//		out.close();
	}


//	public static String ToJson(Object value) {
//		try {
//			return writeMapper.writeValueAsString(value);
//		} catch (Exception var2) {
//			LOG.error("ToJosn error,Object:{}", value, var2);
//			return "";
//		}
//	}
//
//	public static byte[] ToJsonAsBytes(Object value) {
//		try {
//			return writeMapper.writeValueAsBytes(value);
//		} catch (Exception var2) {
//			LOG.error("ToJsonAsBytes error,Object:{}", value, var2);
//			return null;
//		}
//	}

}